<?php
// Copy zingme-sdk directory to your application
// and require below libraries if you need to get user's profile
require_once ('lib/zingme-sdk/BaseZingMe.php');
require_once ('lib/zingme-sdk/ZME_Me.php');
require_once ('lib/zingme-sdk/ZME_User.php');
require_once ('lib/zingme-sdk/ZME_Photo.php');

// define your callback url for Zing Me SDK to redirect user after login
$callback_url = "http://localhost/sdk-test/php/index.php";

// initialize configuration
$config_dev = array(
	'appname' => 'zigo',
	'apikey' => 'e5e7fd50431a48a7b3206e13cdfd8207',
	'secretkey' => 'e4ad44ef86ce4a2f8435438b05cf1471',
	'env' => 'development'
	);

$config_live = array(
		'appname' => 'zigo',
		'apikey' => 'e5e7fd50431a48a7b3206e13cdfd8207',
		'secretkey' => 'e4ad44ef86ce4a2f8435438b05cf1471',
		'env' => 'production'
);
$config = $config_live;
// instantiate ZME_Me object;
$zme_me = new ZME_Me($config);
$zme_user = new ZME_User($config);
$zme_photo = new ZME_Photo($config);

$access_token = $zme_me->getAccessToken();

?>
<html>
  <head><title>Zing Me SDK Web Sample Code</title></head>
  <body>
<?php

// NO access token?
// authenticate user to get authorize code
// then get access token from authorize code
if(! isset($access_token)) {
  $code = $_REQUEST ['code'];
  
  if(isset($code)) {
    $access_token = $zme_me->getAccessTokenFromCode($code)['access_token'];
  } else {
    $login_url = $zme_me->getUrlAuthorized($callback_url);
    echo 'Please <a href="' . $login_url . '">login.</a>';
    exit();
  }
}

echo "1. access token: $access_token";

try {
  // after get access token, continue to get user's profile
  $user_profile = $zme_me->getInfo($access_token);
  echo "<br/>2. profile: ";
  var_dump($user_profile);
  
  // get friend list info, only 50 ids/request allow.
  $friend_ids = $zme_me->getFriends($access_token);
  $friends = $zme_user->getInfo($access_token, array_slice($friend_ids, 0, 50));
  echo "<br/>3. friend list: ";
  var_dump($friends);
  
  // upload photo
  echo "<br/><br/> 4. upload photo with absolute/relative path: ";
//   $data = $zme_photo->upload($access_token, "./test.jpg", "test with relative path");
  $data = $zme_photo->upload($access_token, "/Library/WebServer/Documents/sdk-test/php/test.jpg", "test absolute path");
  var_dump($data);
  
  //post feed
  echo "<br/><br/> 5. post feed to user's wall:";
  
} catch(ZingMeException $e) {
  error_log($e->getType());
  error_log($e->getMessage());
  // if user logged out or access token expired,
  // we'll get an exception, so we'll
  // just ask the user to login again here.
  $login_url = $zme_me->getUrlAuthorized($callback_url);
  echo 'Please <a href="' . $login_url . '">login.</a>';
}

?>
 </body>
</html>  